using Admin2025.Application.Dtos;
using Admin2025.Application.common;
using Admin2025.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Admin2025.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
    private readonly IAppUserService _service;
    public UserController(IAppUserService service)
    {
        _service = service;
    }


    [HttpGet("{id}")]
    public async Task<ApiResult> GetById(Guid id) => ApiResult.Success(await _service.GetByIdAsync(id));

    [HttpPost]
    public async Task<ApiResult> Create([FromBody] CreateUserDto dto)
    {
        await _service.CreateAsync(dto);
        return ApiResult.Success(message: "创建成功");
    }
    [HttpPut("{id}")]
    public async Task<ApiResult> Update(Guid id, [FromBody] UpdateUserDto dto)
    {
        await _service.UpdateAsync(id, dto);
        return ApiResult.Success(message: "修改成功");
    }
    [HttpDelete("{id}")]
    public async Task<ApiResult> Delete(Guid id)
    {
        await _service.DeleteAsync(id);
        return ApiResult.Success(message: "删除成功");
    }
    [HttpPost("{id}/enable")]
    public async Task<ApiResult> Enable(Guid id)
    {
        await _service.EnableAsync(id);
        return ApiResult.Success(message: "启用成功");
    }
    [HttpPost("{id}/disable")]
    public async Task<ApiResult> Disable(Guid id)
    {
        var res = await _service.DisableAsync(id);
        return res;
    }
    [HttpPost("{id}/assign-role")]
    public async Task<ApiResult> AssignRole(Guid id, [FromBody] AssignRoleDto dto)
    {
        await _service.AssignRoleAsync(id, dto.RoleId);
        return ApiResult.Success(message: "分配角色成功");
    }
    [HttpPost("{id}/remove-role")]
    public async Task<ApiResult> RemoveRole(Guid id, [FromBody] AssignRoleDto dto)
    {
        await _service.RemoveRoleAsync(id, dto.RoleId);
        return ApiResult.Success(message: "移除角色成功");
    }
    [HttpPost("{id}/avatar")]
    public async Task<ApiResult> UpdateAvatar(Guid id, [FromBody] UpdateUserDto dto)
    {
        await _service.UpdateAvatarAsync(id, dto.AvatarUrl!);
        return ApiResult.Success(message: "头像更新成功");
    }
    [HttpPost("{id}/nickname")]
    public async Task<ApiResult> UpdateNickname(Guid id, [FromBody] UpdateUserDto dto)
    {
        await _service.UpdateNicknameAsync(id, dto.Nickname!);
        return ApiResult.Success(message: "昵称更新成功");
    }
    [HttpPost("{id}/password")]
    public async Task<ApiResult> ChangePassword(Guid id, [FromBody] ChangePasswordDto dto)
    {
        await _service.ChangePasswordAsync(id, dto.OldPassword, dto.NewPassword);
        return ApiResult.Success(message: "密码修改成功");
    }
    //分页获取所有用户
    // [HttpGet]
    // public async Task<ApiResult> GetUsers([FromQuery] PageRequestDto dto)
    // {
    //     var result = await _service.GetUsersWithRolesAsync(dto);
    //     return ApiResult.SuccessPaged(result);
    // }
    
      [HttpGet]
    public async Task<ApiResult> GetUsers([FromQuery] PageRequestDto dto)
    {
        var result = await _service.GetUsersWithRolesAsync(dto);
        return ApiResult.SuccessPaged(result);
    }
}